package com.example.user.player;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;

import android.widget.MediaController;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.VideoView;
import android.widget.Toast;
import android.view.View.OnClickListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.io.File;


public class AudioActivity extends AppCompatActivity {
    private VideoView videoView=null;
    private ArrayList<Map<String,Object>> listems=null;
    private ArrayList<AudioMedia> aduiolist=null;
    private int currentposition=-1;
    private boolean isPlay=false;
    public static SeekBar audioSeekBar = null;//定义进度条
    MediaController mediaco;
    public static TextView textView = null;
    private  ListView AduioListView=null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);

        videoView=(VideoView)findViewById(R.id.videoshow);
        AduioListView=(ListView)findViewById(R.id.video_list);
        aduiolist=scanAllAudioFiles();
        mediaco=new MediaController(this);
        listems=new ArrayList<Map<String,Object>>();
        for (Iterator iterator = aduiolist.iterator(); iterator.hasNext();) {
            Map<String, Object> map = new HashMap<String, Object>();
            AudioMedia mp4Info = (AudioMedia) iterator.next();
            map.put("title", mp4Info.getTitle());
            map.put("artist", mp4Info.getArtist());
            map.put("album", mp4Info.getAlbum());
            map.put("duration", mp4Info.getTime());
            map.put("size", mp4Info.getSize());
            map.put("url", mp4Info.getUrl());
            map.put("bitmap", R.drawable.timg);
            listems.add(map);
        }
        SimpleAdapter vSimpleAdapter=new SimpleAdapter(
                this,listems,R.layout.video_xml,
                new String[]{"bitmap","title","artist","size","duration"},
                new int[]{R.id.audio_imageView,R.id.audio_title,R.id.audio_player,R.id.audio_size,
                        R.id.audio_duration}

        );
        AduioListView.setAdapter(vSimpleAdapter);
        AduioListView.setOnItemClickListener(new  AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentposition=position;
                String url=aduiolist.get(position).getUrl();
                videoView.setMediaController(mediaco);
                mediaco.setMediaPlayer(videoView);
                videoView.setVideoPath(url);
                videoView.start();

            }
        });

    }
   // public void init(){

//  }
    public ArrayList<AudioMedia> scanAllAudioFiles(){
        //生成动态数组，并且转载数据
        ArrayList<AudioMedia> mylist = new ArrayList<AudioMedia>();

        Cursor cursor =getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
        //遍历媒体数据库
        if(null== cursor)
        {
            return null;
        }
        if(cursor.moveToFirst()){
            while (!cursor.isAfterLast()) {
                //歌曲编号
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                //歌曲标题
                String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                //歌曲的专辑名：MediaStore.Audio.Media.ALBUM
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));

                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                //歌曲文件的路径 ：MediaStore.Audio.Media.DATA
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                //歌曲的总播放时长 ：MediaStore.Audio.Media.DURATION
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                //歌曲文件的大小 ：MediaStore.Audio.Media.SIZE
                Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));


                if (size >1024*800){//大于800K
                    AudioMedia audioMedia = new AudioMedia();
                    audioMedia.setId(id);
                    audioMedia.setArtist(artist);
                    audioMedia.setSize(size);
                    audioMedia.setTitle(tilte);
                    audioMedia.setTime(duration);
                    audioMedia.setUrl(url);
                    audioMedia.setAlbum(album);

                    Log.e("music disName=", audioMedia.getTitle());//
                    mylist.add(audioMedia);

                }
                cursor.moveToNext();
            }
        }
        cursor.close();
        return mylist;
    }

}
